<?php

/**
 * Product form base class.
 *
 * @method Product getObject() Returns the current form's model object
 *
 * @package    Onnobostro
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id$
 */
abstract class BaseProductForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                      => new sfWidgetFormInputHidden(),
      'product_category_id'     => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('ProductCategory'), 'add_empty' => false)),
      'product_gift_id'         => new sfWidgetFormInputText(),
      'name'                    => new sfWidgetFormInputText(),
      'item_code'               => new sfWidgetFormInputText(),
      'description'             => new sfWidgetFormTextarea(),
      'price'                   => new sfWidgetFormInputText(),
      'body_length'             => new sfWidgetFormInputText(),
      'half_chest_width'        => new sfWidgetFormInputText(),
      'special_rate'            => new sfWidgetFormInputText(),
      'discount_rate1'          => new sfWidgetFormInputText(),
      'discount_rate2'          => new sfWidgetFormInputText(),
      'gift_qty'                => new sfWidgetFormInputText(),
      'is_discount'             => new sfWidgetFormInputCheckbox(),
      'is_new'                  => new sfWidgetFormInputCheckbox(),
      'is_active'               => new sfWidgetFormInputCheckbox(),
      'image_id'                => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Image'), 'add_empty' => false)),
      'selling_qty'             => new sfWidgetFormInputText(),
      'created_at'              => new sfWidgetFormDateTime(),
      'updated_at'              => new sfWidgetFormDateTime(),
      'position'                => new sfWidgetFormInputText(),
      'created_by'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('CreatedBy'), 'add_empty' => true)),
      'updated_by'              => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('UpdatedBy'), 'add_empty' => true)),

        'product_size_list_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'ProductSize', 'expanded' => true)),
        'product_color_list_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'ProductColor', 'expanded' => true)),
        'lr_articles_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'LrArticle', 'expanded' => true)),
        'lr_newsletters_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'LrNewsletter', 'expanded' => true)),
        'customar_orders_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CustomarOrder', 'expanded' => true)),
      ));

    $this->setValidators(array(
      'id'                      => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'product_category_id'     => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('ProductCategory'))),
      'product_gift_id'         => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'name'                    => new sfValidatorString(array('max_length' => 100)),
      'item_code'               => new sfValidatorString(array('max_length' => 50)),
      'description'             => new sfValidatorString(),
      'price'                   => new sfValidatorString(array('max_length' => 100)),
      'body_length'             => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'half_chest_width'        => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'special_rate'            => new sfValidatorNumber(array('required' => false)),
      'discount_rate1'          => new sfValidatorNumber(array('required' => false)),
      'discount_rate2'          => new sfValidatorNumber(array('required' => false)),
      'gift_qty'                => new sfValidatorInteger(array('required' => false)),
      'is_discount'             => new sfValidatorBoolean(array('required' => false)),
      'is_new'                  => new sfValidatorBoolean(array('required' => false)),
      'is_active'               => new sfValidatorBoolean(array('required' => false)),
      'image_id'                => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Image'))),
      'selling_qty'             => new sfValidatorInteger(array('required' => false)),
      'created_at'              => new sfValidatorDateTime(),
      'updated_at'              => new sfValidatorDateTime(),
      'position'                => new sfValidatorInteger(array('required' => false)),
      'created_by'              => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('CreatedBy'), 'required' => false)),
      'updated_by'              => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('UpdatedBy'), 'required' => false)),
        'product_size_list_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'ProductSize', 'required' => false)),
        'product_color_list_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'ProductColor', 'required' => false)),
        'lr_articles_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'LrArticle', 'required' => false)),
        'lr_newsletters_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'LrNewsletter', 'required' => false)),
        'customar_orders_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CustomarOrder', 'required' => false)),
      ));

    /*
     * Embed Media form for image_id
     */
    $this->embedForm('image_id_form', $this->createMediaFormForImageId());
    unset($this['image_id']);

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Product', 'column' => array('item_code')))
    );

    $this->widgetSchema->setNameFormat('product[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
    
    // Unset automatic fields like 'created_at', 'updated_at', 'position'
    // override this method in your form to keep them
    parent::unsetAutoFields();
  }

  /**
   * Creates a DmMediaForm instance for image_id
   *
   * @return DmMediaForm a form instance for the related media
   */
  protected function createMediaFormForImageId()
  {
    return DmMediaForRecordForm::factory($this->object, 'image_id', 'Image', $this->validatorSchema['image_id']->getOption('required'));
  }

  protected function doBind(array $values)
  {
    $values = $this->filterValuesByEmbeddedMediaForm($values, 'image_id');
    parent::doBind($values);
  }
  
  public function processValues($values)
  {
    $values = parent::processValues($values);
    $values = $this->processValuesForEmbeddedMediaForm($values, 'image_id');
    return $values;
  }
  
  protected function doUpdateObject($values)
  {
    parent::doUpdateObject($values);
    $this->doUpdateObjectForEmbeddedMediaForm($values, 'image_id', 'Image');
  }

  public function getModelName()
  {
    return 'Product';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['product_size_list_list']))
    {
      $this->setDefault('product_size_list_list', $this->object->ProductSizeList->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['product_color_list_list']))
    {
      $this->setDefault('product_color_list_list', $this->object->ProductColorList->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['lr_articles_list']))
    {
      $this->setDefault('lr_articles_list', $this->object->LrArticles->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['lr_newsletters_list']))
    {
      $this->setDefault('lr_newsletters_list', $this->object->LrNewsletters->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['customar_orders_list']))
    {
      $this->setDefault('customar_orders_list', $this->object->CustomarOrders->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['medias_list']))
    {
      $this->setDefault('medias_list', $this->object->Medias->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveProductSizeListList($con);
    $this->saveProductColorListList($con);
    $this->saveLrArticlesList($con);
    $this->saveLrNewslettersList($con);
    $this->saveCustomarOrdersList($con);
    $this->saveMediasList($con);

    parent::doSave($con);
  }

  public function saveProductSizeListList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['product_size_list_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->ProductSizeList->getPrimaryKeys();
    $values = $this->getValue('product_size_list_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('ProductSizeList', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('ProductSizeList', array_values($link));
    }
  }

  public function saveProductColorListList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['product_color_list_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->ProductColorList->getPrimaryKeys();
    $values = $this->getValue('product_color_list_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('ProductColorList', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('ProductColorList', array_values($link));
    }
  }

  public function saveLrArticlesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['lr_articles_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->LrArticles->getPrimaryKeys();
    $values = $this->getValue('lr_articles_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('LrArticles', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('LrArticles', array_values($link));
    }
  }

  public function saveLrNewslettersList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['lr_newsletters_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->LrNewsletters->getPrimaryKeys();
    $values = $this->getValue('lr_newsletters_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('LrNewsletters', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('LrNewsletters', array_values($link));
    }
  }

  public function saveCustomarOrdersList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['customar_orders_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CustomarOrders->getPrimaryKeys();
    $values = $this->getValue('customar_orders_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CustomarOrders', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CustomarOrders', array_values($link));
    }
  }

  public function saveMediasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['medias_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Medias->getPrimaryKeys();
    $values = $this->getValue('medias_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Medias', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Medias', array_values($link));
    }
  }

}